package com.mydemo.game.prepareStage;

import com.mydemo.entity.Car;
import com.mydemo.entity.Driver;
import com.mydemo.entity.Save;
import com.mydemo.print.Print;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class DriverToCar {
    static Scanner scanner = new Scanner(System.in);

    public static final Save driverToCar(Save save) {
        System.out.println("********************");
        System.out.println("为司机指派车辆");
        System.out.println("当前无司机的车辆");
        List<Car> carList = new ArrayList<>();
        if (save.getCarList() != null) {
            for (Car car :
                    save.getCarList()) {
                if (!car.isDrive()) {
                    carList.add(car);
                    Print.printCar(car);
                }
            }
        }
        System.out.println("请选择一辆车,输入序号选择，输入0退出");
        int chooseCar = scanner.nextInt();
        Car selectedCar = new Car();
        if (chooseCar == 0) {
            return save;
        } else {
            selectedCar = selectCar(carList, chooseCar);
        }
        System.out.println("当前无车辆的司机");
        List<Driver> driverList = new ArrayList<>();
        for (Driver driver :
                save.getDriverList()) {
            if (driver.getCar() == 0) {
                driverList.add(driver);
                Print.printDriver(driver);
            }
        }
        System.out.println("请为他指派司机，输入0退出");
        int chooseDriver = scanner.nextInt();
        if (chooseDriver == 0) {
            return save;
        } else {
            selectedCar.setDrive(true);
            Driver driver = selectDriver(driverList, chooseDriver);
            driver.setCar(selectedCar.getId());
        }
        return save;
    }

    private static final Car selectCar(List<Car> carList, int id) {
        for (Car car :
                carList) {
            if (car.getId() == id) return car;
        }
        return null;
    }

    private static final Driver selectDriver(List<Driver> driverList, int id) {
        for (Driver driver :
                driverList) {
            if (driver.getId() == id) return driver;
        }
        return null;
    }
}
